package com.yisu.expressway.service_district;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.model.ShopDetailObj;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;

public class ShopDetailActivity extends BaseActivity {

    final static String SERVICE_AREA_ID = "service_area_id";
    final static String SHOP_ID = "shop_id";
    final static String SHOP_NAME = "shop_name";

    @Bind(R.id.tv_product_sort)
    protected TextView mTv_businessScope;
    @Bind(R.id.tv_open_time)
    protected TextView mTv_openingTime;
    @Bind(R.id.tv_contact_number)
    protected TextView mTv_number;
    @Bind(R.id.tv_news)
    protected TextView mTv_notice;
    @Bind(R.id.bonus_root)
    protected RelativeLayout mBonusRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_detail);
        getShopDetail(getIntent());
    }

    private void getShopDetail(Intent intent){

        int serviceAreaId = intent.getIntExtra(SERVICE_AREA_ID,-1);
        int ShopId = intent.getIntExtra(SHOP_ID,-1);

        if(serviceAreaId == -1 || ShopId == -1){
            return;
        }

        setTitle(intent.getStringExtra(SHOP_NAME));

        Map<String, Object> params = new HashMap();
        params.put("serviceId", serviceAreaId);
        params.put("shopId",ShopId);

        ApiClientHelper.postApi(ApiSetting.getShopDetail(), new TypeToken<ShopDetailObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ShopDetailObj>>() {
            @Override
            public void onResponse(ApiResult<ShopDetailObj> result) {
                if (!result.success) {
                    ToastUtil.displayLongToastMsg(ShopDetailActivity.this, result.getMsg());
                    return;
                }

                ShopDetailObj detailObj = result.getResult();
                if(detailObj == null ){
                    return;
                }


                mTv_businessScope.setText(detailObj.businessScope);
                mTv_openingTime.setText(detailObj.businessStartTime + "-" + detailObj.businessEndTime);
                //未显示营业状态
                mTv_number.setText(detailObj.contactPhone);
                if(!StringUtil.isNullOrEmpty(detailObj.remark)) {
                    mTv_notice.setText(detailObj.remark);
                }else {
                    mTv_notice.setText(R.string.no_bonus);
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToastUtil.displayShortToastMsg(ShopDetailActivity.this, volleyError.getMessage());
            }
        }, this);
    }

    public static void launch(Context context, int serviceAreaId, int shopId,String shopName){
        Intent mIntent = new Intent(context,ShopDetailActivity.class);
        mIntent.putExtra(SERVICE_AREA_ID,serviceAreaId);
        mIntent.putExtra(SHOP_ID,shopId);
        mIntent.putExtra(SHOP_NAME,shopName);
        context.startActivity(mIntent);
    }
}
